<template>
    <swiper
      ref="swiper"
      :key="refreshKey"
      :options="swiperOption"
      v-if="bannerList.length > 0"
    >
      <swiper-slide
        class="swiper-slide"
        v-for="(item, index) in bannerList"
        :key="index"
      >
        <div
          :style="{
            'background-image':
              'url(' + tools.getImgUrl(item.listPic, true) + ')',
          }"
          class="poster"
        ></div>
        <div class="cont">
          <div class="title">{{ item.title }}</div>
          <div class="number">播放{{ item.views }}次</div>
        </div>
      </swiper-slide>
    </swiper>
</template>

<script>
/**
 * 首页banner 分为活动的及固定的
 * 活动的通过接口获取，固定的与后台约定栏目ID主动获取
 */
import moment from "moment";
import "swiper/dist/css/swiper.css";
import StorageKeys from "@/common/const/StorageKeys";
import tools from "../../../../utils/tools";
import _ from "lodash";
import { mapState } from "vuex";
let vm = null;
export default {
  name: "banner",
  data() {
    return {
      bannerList: [],
      pages: [],
      refreshKey: "",
      sliderinit: {
        currentPage: 0, //当前页码
        thresholdDistance: 500, //滑动判定距离
        thresholdTime: 100, //滑动判定时间
        autoplay: 1000, //自动滚动[ms]
        loop: true, //循环滚动
        direction: "vertical", //方向设置，垂直滚动
        infinite: 1, //无限滚动前后遍历数
        slidesToScroll: 1, //每次滑动项数
      },
      swiperOption1: {
        slidesPerView: 1,
        spaceBetween: 0,
        loopFillGroupWithBlank: true,
        //显示分页
        pagination: {
          el: ".swiper-pagination",
        },
        //设置点击箭头
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        loop: true,
        //自动轮播
        autoplay: {
          delay: 5000,
          //当用户滑动图片后继续自动轮播
          disableOnInteraction: false,
          autopalyDisableOnInteraction: false,
          stopOnLastSlide: false,
        },
        on: {
          click() {
            let currentIndex = this.realIndex;
            vm.goDetail(vm.bannerList[currentIndex]);
          },
        },
      },
      swiperOption2: {
        slidesPerView: 1,
        spaceBetween: 0,
        loopFillGroupWithBlank: true,
        //显示分页
        pagination: {
          el: ".swiper-pagination",
        },
        //设置点击箭头
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        loop: false,
        //自动轮播
        autoplay: {
          delay: 5000,
          //当用户滑动图片后继续自动轮播
          disableOnInteraction: false,
          autopalyDisableOnInteraction: false,
          stopOnLastSlide: false,
        },
        on: {
          click() {
            let currentIndex = this.realIndex;
            vm.goDetail(vm.bannerList[currentIndex]);
          },
        },
      },
    };
  },
  computed: {
    swiperOption(){
      var len = this.bannerList.length
      if(len == 1){
        return this.swiperOption2
      }else{
        return this.swiperOption1
      }
    },
  },
  created() {
    vm = this;
    vm.$nextTick(() => {
      vm.init();
    });
  },
  activated() {
    this.$nextTick(() => {
      if (vm.$refs.swiper) {
        /*防止轮播异常*/
        vm.$refs.swiper.swiper.destroy();
        /*确保轮播正常进行*/
        vm.$refs.swiper.mountInstance();
      }
    });
  },
  methods: {
    init() {
      this.fetchList();
    },
    fetchList(id) {
      let data = {
        channelId: "df6ff787ac0f4eaf8ccff37fc107cf60",
        pageNo: 1,
        pageSize: 3,
      };
      this.fetchService.contentPage(data).then((res) => {
        if (res.code !== 0) {
          return this.tools.alert(res.message || res.resp_msg);
        }
        this.bannerList = res.data.page;
        if (this.bannerList.length === 1) {
          this.swiperOption.autoplay = false;
        } else {
          this.swiperOption.autoplay = {
            delay: 5000,
            //当用户滑动图片后继续自动轮播
            disableOnInteraction: false,
            autopalyDisableOnInteraction: false,
            stopOnLastSlide: false,
          };
        }
        console.log("this.bannerList", this.bannerList);
      });
    },
    goDetail(item) {
      /* this.$store.state.videoDetail = item;
      this.$store.state.newsDetail = item;
      this.$router.push({
        name: "videoDetail",
        query: { id: item.id },
      }); */
      this.$store.state.isVideo = true;
      this.$store.state.videoDetail = item;
      this.$store.state.newsDetail = item;
      // 缓存专辑数据
      this.$store.state.zhuanjiList = this.list;
      // 带上专辑标识，详情页进行连续播放
      this.$router.push({
        name: "videoDetail",
        query: { id: item.id, zhuanji: 1 },
      });
    },
    getImgUrl(id) {
      return this.tools.getImgUrl(id);
    },
  },
};
</script>

<style scoped lang="scss">
@import "@/common/stylus/moon.scss";
.banner-text {
  font-size: 15PX;
  overflow: hidden;
  width: 70%;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 10px 15px;
}
.cont .title {
  position: relative;
  height: 1em;
  overflow: hidden;
}
.today {
  position: absolute;
  top: 64%;
  font-size: 0.6rem;
  width: 100%;
  text-align: center;
  color: #c7c5c5;
  text-shadow: 5px 2px 6px #000;
  font-family: \5b8b\4f53;
  font-weight: bold;
}
.sfd-title{
  font-size: 14PX;
}
.poster {
  width: calc(100vw - #{pxToRem(60px)});
  margin: calc(4vw * 9 / 16) pxToRem(30px);
  height: calc((100vw - #{pxToRem(60px)}) * 9 / 16);
  position: absolute;
  left: 0;
  z-index: 2;
  background: url("") no-repeat center;
  background-size: cover;
  border-radius: pxToRem(10px);
}
.cont {
  text-align: center;
  position: absolute;
  bottom: -5px;
  left: 0;
  font-size: 14px;
  height: 60px;
  width: 100%;
  text-overflow: ellipsis;
  color: #000;
  text-shadow: 0px 5px rgba(#000000, 0.5);
  white-space: nowrap;
  font-size: 15PX;
  padding: 0 pxToRem(30px);
  .title {
    width: 70%;
    color: #000;
    float: left;
    text-align: left;
    padding: 0 10px;
    /*font-weight: bold;*/
    text-shadow: none !important;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    display: -webkit-box;
    display: -ms-flexbox;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -ms-flex-direction: column;
    flex-direction: column;
  }
  .number {
    width: 30%;
    color: #999999;
    text-align: right;
    //padding: 0 30px;
    font-size: 14PX;
    float: right;
    text-shadow: none !important;
  }
}
</style>
